/* eslint-disable */
import Vue from 'vue'
import axios from 'axios'

import iView from 'iview'
import router from './router/index'
import store from './store'
import App from './app.vue'
import 'iview/dist/styles/iview.css'
import '@/styles/common.less'
import VueHtml5Editor from 'vue-html5-editor'

Vue.use(iView)
Vue.use(VueHtml5Editor, {
  image: {
    sizeLimit: 10 * 1024 * 1024
  },
  icons: {
    text: 'icon icon-pencil',
    color: 'icon icon-paint-brush',
    font: 'icon icon-font',
    align: 'icon-align-center',
    list: 'icon icon-list',
    link: 'icon icon-chain',
    unlink: 'icon icon-chain-broken',
    tabulation: 'icon icon-table',
    image: 'icon icon-file-image-o',
    hr: 'icon icon-minus',
    eraser: 'icon icon-eraser',
    undo: 'icon-undo icon',
    'full-screen': 'icon icon-arrows-alt',
    info: 'icon icon-info'
  },
  language: 'zh-cn',
  i18n: {
    'zh-cn': {
      'align': '对齐方式',
      'image': '图片',
      'list': '列表',
      'link': '链接',
      'unlink': '去除链接',
      'table': '表格',
      'font': '文字',
      'full screen': '全屏',
      'text': '排版',
      'eraser': '格式清除',
      'info': '关于',
      'color': '颜色',
      'please enter a url': '输入图片网络地址',
      'create link': '创建链接',
      'bold': '加粗',
      'italic': '倾斜',
      'underline': '下划线',
      'strike through': '删除线',
      'subscript': '上标',
      'superscript': '下标',
      'heading': '标题',
      'font name': '字体',
      'font size': '文字大小',
      'left justify': '左对齐',
      'center justify': '居中',
      'right justify': '右对齐',
      'ordered list': '有序列表',
      'unordered list': '无序列表',
      'fore color': '文字',
      'background color': '背景',
      'row count': '行数',
      'column count': '列数',
      'save': '确定',
      'upload': '上传本地图片',
      'progress': '进度',
      'unknown': '未知',
      'please wait': '请稍等',
      'error': '错误',
      'abort': '中断',
      'reset': '重置'
    }
  },
  visibleModules: [
    'text',
    'color',
    'font',
    'align',
    'list',
    'link',
    'unlink',
    'tabulation',
    'image',
    'hr',
    'eraser',
    'undo',
    'full-screen'
  ]
})

axios.interceptors.request.use(request => {
  iView.LoadingBar.start()
  var ticket = localStorage.getItem('ticket')
  request.headers['Authorization'] = 'cookies ' + ticket
  return request
})
axios.interceptors.response.use(
  function (response) {
    iView.LoadingBar.finish()
    if (response.data.success === false) {
      if (response.data.code === 0 && router.app.$route.meta.anonymous !== true) {
        router.push({
          name: 'logout'
        })
      } else {
        let errorMessage = response.data.message || '出错了，请联系管理员'
        iView.Message.error(errorMessage)
      }
    }
    return response
  },
  function (err) {
    return new Promise(function (resolve, reject) {
      if (err.response.status === 401 || err.response.status === 403) {
        router.push({
          path: '/login'
        })
      } else {
        throw err
      }
    })
  }
)

Vue.prototype.$http = axios
Vue.component('iIcon', iView.Icon)
Vue.component('iRow', iView.Row)
Vue.component('iMenuGroup', iView.MenuGroup)
Vue.component('iMenuItem', iView.MenuItem)
Vue.component('iCard', iView.Card)
Vue.component('iFormItem', iView.FormItem)
Vue.component('iRadioGroup', iView.RadioGroup)
Vue.component('iRadio', iView.Radio)
Vue.component('iTag', iView.Tag)
Vue.component('iDropdown', iView.Dropdown)
Vue.component('iDropdownMenu', iView.DropdownMenu)
Vue.component('iDropdownItem', iView.DropdownItem)
Vue.component('iAvatar', iView.Avatar)
Vue.component('iBreadcrumb', iView.Breadcrumb)
Vue.component('iBreadcrumbItem', iView.BreadcrumbItem)
Vue.component('iSubmenu', iView.Submenu)
Vue.component('iPage', iView.Page)
Vue.component('iCheckbox', iView.Checkbox)
Vue.component('iUpload', iView.Upload)
Vue.component('iModal', iView.Modal)
Vue.component('iAutoComplete', iView.AutoComplete)
Vue.component('iDatePicker', iView.DatePicker)
Vue.component('iCarousel', iView.Carousel)
Vue.component('iCarouselItem', iView.CarouselItem)
Vue.component('iTabs', iView.Tabs)
Vue.component('iTabPane', iView.TabPane)
Vue.component('iAffix', iView.Affix)
Vue.component('iTooltip', iView.Tooltip)
Vue.component('iSpin', iView.Spin)
Vue.component('iAffix', iView.Affix)

Vue.directive('reply', {
  inserted(el, arg, vm) {
    var container = el.parentNode.parentNode

    el.addEventListener('click', () => {
      const span = el.children[el.children.length - 1]
      if (span.innerText === '回复') {
        span.innerText = '收起'
        var div = document.createElement('div')
        container.appendChild(div)
        var res = Vue.compile("<form class='new-message clearfix' @submit.prevent='onSubmit()'><i-input v-model='form.content' type='textarea'></i-input><i-button type='primary' html-type='submit'>提交</i-button></form>")

        var objectId = vm.context.objectId
        new Vue({
          el: div,
          data: {
            form: {
              objectId: objectId,
              rootId: arg.value.rootId || arg.value.id,
              targetId: arg.value.createdBy.id,
              content: null
            }
          },
          methods: {
            onSubmit() {
              vm.context.$store.dispatch('saveComment', {
                form: this.form,
                callback() {
                  el.click()
                }
              })
            }
          },
          render: res.render,
          staticRenderFns: res.staticRenderFns
        })
      } else {
        span.innerText = '回复'
        container.removeChild(container.childNodes[container.childNodes.length - 1])
      }
      // container.appendChild()
    })
    // console.log(vm.context.search)
  }
})

window.__version = __version

new Vue({
  el: '#app',
  router: router,
  store: store,
  render: h => h(App)
})
